<template>
  <div class="container">
    <div class="app-container">
      组织架构
      <el-tree :data="depts" :props="defaultProps" :expand-on-click-node="false" :default-expand-all="true" @node-click="handleNodeClick">
        <template slot-scope="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="hCommand($event , data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <add_dept ref="dept" :close-name="closeName" :show-dept.sync="showDept" :close-id="closeId" @updateDepartment="uplistData" />
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { arr2tree } from '@/utils/index'
import add_dept from './components/add_dept.vue'
export default {
  name: 'Department',
  components: {
    add_dept

  },
  data() {
    return {

      closeId: '',
      closeName: false,
      depts: [{
        name: '传智教育',
        managerName: 'A0',
        children: [
          { name: '总裁办', managerName: 'A1' },
          { name: '行政部', managerName: 'A2' },
          { name: '财务部', managerName: 'A3' }
        ]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDept: false
    }
  },
  created() {
    this.uplistData()
  },
  methods: {
    hCommand(name, id) {
      this.closeId = id

      if (name === 'add') {
        this.showDept = true
        this.closeName = false
      } else if (name === 'del') {
        this.doDel(id)
      } else if (name === 'edit') {
        this.closeName = true
        this.showDept = true
        this.$nextTick(() => {
          this.$refs.dept.getRevise()
        })
      }
    },

    doDel(id) {
      this.$confirm('是否确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delClose(id)
        this.uplistData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async uplistData() {
      const res = await getDepartment()
      this.depts = arr2tree(res.data, 1)
    },
    async delClose(id) {
      await delDepartment(id)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
    },
    handleNodeClick(data) {
      // console.log(data)
    }

  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
